package org.leo.dm.util.beanvalidate;

import java.util.HashMap;
import java.util.Map;

import org.leo.dm.util.comm.Dom4jUtils;
import org.leo.dm.util.exception.DmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 初始化规则
 * 
 * @author xiao_h
 * 
 */
public class InitRules {
	private static Logger log = LoggerFactory.getLogger(InitRules.class);

	/**
	 * key为规则名称，value为规则内容
	 */
	private static Map<String, String> rules = new HashMap<String, String>();

	/**
	 * 初始化规则集合
	 */
	static {
		try {
			// 初始化通用规则
			log.info("初始化校验文件集合.");
		} catch (Exception e) {
			new DmException("初始化校验文件集合出错！", e);
		}
	}

	public static Map<String, String> getRules() {
		return rules;
	}

	public static void setRules(Map<String, String> rules) {
		InitRules.rules = rules;
	}

	/**
	 * 获取指定规则方法
	 * 
	 * @param ruleName
	 * @param relativeRulePath
	 * @return
	 */
	public static String init(String ruleName, String relativeRulePath) {
		if ("run".equals(CommonConfig.getRunOrDebug()) && getRules().containsKey(ruleName)) {
			return getRules().get(ruleName);
		} else {
			try {
				log.info("初始化Bean属性校验配置 加载校验文件路径：" + relativeRulePath);
				getRules().put(ruleName, Dom4jUtils.parseFIOToXml(relativeRulePath).asXML());
			} catch (Exception e) {
				new DmException("初始化校验文件出错！", e);
			}
		}
		return getRules().get(ruleName);
	}
}
